#include "mainwindow.h"
#include "ui_mainwindow.h"
#include<QPainter>
#include<QKeyEvent>
#include<QTimer>
#include"snake.h"
#include"food.h"
#include<QMessageBox>
#include<QLabel>
#include<QPushButton>

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
    setFixedSize(1000,600);
    setWindowIcon(QIcon(":/res/icon.jpg"));


    ispause=false;
    timer->start(timerspeed);
    connect(timer,&QTimer::timeout,[=]()
    {if(isgameover())
        {
            timer->stop();
            gameover();
            return;
        }
        snake.move(key);
        if(food.getfoodx()==snake.snakevec.at(0).x&&food.getfoody()==snake.snakevec.at(0).y)
        {
            core++;
            timerspeed+=100;
            food.creatfood();
        }
        else
        {
            snake.snakevec.pop_back();
        }
        update();
    });
}

MainWindow::~MainWindow()
{
    delete ui;
}
void MainWindow::gameover()
{
    timer->stop();
    QMessageBox::information(this,"failed","gameover");
}

void MainWindow::paintEvent(QPaintEvent *)
{
    QPainter p(this);
    //背景
    p.setBrush(Qt::green);
    p.drawRect(0,0,600,600);
    //墙
    p.setBrush(Qt::gray);
    p.drawRect(600,0,25,600);
    //得分
    p.setPen(Qt::black);
    p.setFont(QFont("Arial",20));
    p.drawText(760,100,"得分："+QString::number(core));
    //画食物
    p.setBrush(Qt::red);
    p.drawRect(QRect(food.getfoodx(),food.getfoody(),25,25));
    //画蛇
    p.setBrush(Qt::black);
    for(auto snakenodei:snake.snakevec)
    {
        p.drawRect(QRect(snakenodei.x,snakenodei.y,25,25));
    }
    bugflag=false;
}
void MainWindow::keyPressEvent(QKeyEvent *ev)
{
    if(bugflag)
        return;
    switch (ev->key())
    {
    case Qt::Key_W:
        if(key=='s')
            break;
        key='w';
        bugflag=true;
        break;
    case Qt::Key_S:
        if(key=='w')
            break;
        key='s';
        bugflag=true;
        break;
    case Qt::Key_A:
        if(key=='d')
            break;
        key='a';
        bugflag=true;
        break;
    case Qt::Key_D:
        if(key=='a')
            break;
        key='d';
        bugflag=true;
        break;
    default:
        timer->start(timerspeed);
    }
}
bool MainWindow::isgameover()
{
    if(snake.snakevec.at(0).x>=24*25||snake.snakevec.at(0).x<0||snake.snakevec.at(0).y>=24*25||snake.snakevec.at(0).y<0)
    {
        return true;
    }
    for(int i=1;i<snake.snakevec.size();i++)
    {
        if(snake.snakevec.at(0).x==snake.snakevec.at(i).x&&snake.snakevec.at(0).y==snake.snakevec.at(i).y)
        {
            return true;
        }
    }
    return false;
}

void MainWindow::on_pushButton_clicked()
{
    snake.reset();
    core=0;
    key='d';
    timerspeed=200;
    timer->start(timerspeed);
    isgameover();
}

void MainWindow::on_pushButton_2_clicked()
{
    ispause=!ispause;
    if(ispause)
    {
        timer->stop();
    }
    else
    {
        timer->start(timerspeed);
    }
}
